<?php
session_start();
if (empty($_SESSION['username'])) {
    echo '<script type="text/javascript">window.location.href="../index.php";</script>';
} else {
    $now = time();
    if ($now > $_SESSION['expire']) {
        session_destroy();
        echo '<script type="text/javascript">var r=confirm("Session expire (30 mins)!"); if(r==true){window.location.href="../index.php";}else{window.location.href="index.php";}</script>';
    } else {
        require '../model-db-connection/config.php';
        $config = require '../model-db-connection/qrc_conf.properties.php';
    }
}
?>
<!DOCTYPE html>
<html lang="en">

    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>QRC - Building Management</title>
        <link rel="icon" type="image/ico" href="../images/favicon.ico" />
        <!-- Bootstrap Core CSS -->
        <link href="../css/bootstrap.min.css" rel="stylesheet">
        <link href="../css/jquery.toastmessage.css" rel="stylesheet">

        <!-- MetisMenu CSS -->
        <link href="../css/plugins/metisMenu/metisMenu.min.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="../css/sb-admin-2.css" rel="stylesheet">
        <!-- Custom Fonts -->
        <link href="../font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
        <link rel="stylesheet" href="../css/minoral.css">
        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
        <style type="text/css">
            .spinner {
                width: 70px;
                text-align: center;
            }
            #spinnerCE {
                width: 70px;
                text-align: center;
            }
            #spinnerCE > div {
                width: 18px;
                height: 18px;
                background-color: #333;

                border-radius: 100%;
                display: inline-block;
                -webkit-animation: bouncedelay 1.4s infinite ease-in-out;
                animation: bouncedelay 1.4s infinite ease-in-out;
                /* Prevent first frame from flickering when animation starts */
                -webkit-animation-fill-mode: both;
                animation-fill-mode: both;
            }
            .spinner > div {
                width: 18px;
                height: 18px;
                background-color: #333;

                border-radius: 100%;
                display: inline-block;
                -webkit-animation: bouncedelay 1.4s infinite ease-in-out;
                animation: bouncedelay 1.4s infinite ease-in-out;
                /* Prevent first frame from flickering when animation starts */
                -webkit-animation-fill-mode: both;
                animation-fill-mode: both;
            }
            #spinnerCE .bounce1 {
                -webkit-animation-delay: -0.32s;
                animation-delay: -0.32s;
            }

            #spinnerCE .bounce2 {
                -webkit-animation-delay: -0.16s;
                animation-delay: -0.16s;
            }
            .spinner .bounce1 {
                -webkit-animation-delay: -0.32s;
                animation-delay: -0.32s;
            }

            .spinner .bounce2 {
                -webkit-animation-delay: -0.16s;
                animation-delay: -0.16s;
            }
            @-webkit-keyframes bouncedelay {
                0%, 80%, 100% { -webkit-transform: scale(0.0) }
                40% { -webkit-transform: scale(1.0) }
            }

            @keyframes bouncedelay {
                0%, 80%, 100% { 
                    transform: scale(0.0);
                    -webkit-transform: scale(0.0);
                } 40% { 
                    transform: scale(1.0);
                    -webkit-transform: scale(1.0);
                }
            }

        </style>
    </head>

    <body>

        <div id="wrapper">

            <!-- Navigation -->
            <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="../project-page/index-dashboard.php">QRC-Building Management</a>
                </div>
                <!-- /.navbar-header -->

                <ul class="nav navbar-top-links navbar-right">
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-user fa-fw"></i> <?= $_SESSION['username']; ?> <i class="fa fa-caret-down"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="#"><i class="fa fa-user fa-fw"></i> User Profile</a>
                            </li>
                            <li><a href="../setting-page/index-setting.php"><i class="fa fa-gear fa-fw"></i> Settings</a>
                            </li>
                            <li class="divider"></li>
                            <li><a href="#" id="logout_click"><i class="fa fa-sign-out fa-fw"></i> Logout</a>
                            </li>
                        </ul>
                        <!-- /.dropdown-user -->
                    </li>
                    <!-- /.dropdown -->
                </ul>
                <!-- /.navbar-top-links -->

                <div class="navbar-default sidebar" role="navigation">
                    <div class="sidebar-nav navbar-collapse">
                        <div id="left-menu-load"></div>
                    </div>
                    <!-- /.sidebar-collapse -->
                </div>
                <!-- /.navbar-static-side -->
            </nav>

            <div id="page-wrapper">
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">Billing (ใบเสร็จ)</h1>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
                <!-- /.row -->
                <div class="row" id="load_all_status">
                </div>

                <div class="row">
                    <div class="col-lg-10" id="search_panel">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <i class="fa fa-bar-chart-o fa-fw"></i> Generate Billing
                                <div class="pull-right">
                                    <button type="button" id="create_billing" class="btn btn-primary btn-xs" style="width: 100px" data-toggle="dropdown">Create (สร้าง)</button>
                                    <button type="button" id="close_search_panel" class="btn btn-default btn-xs" style="width: 100px" data-toggle="dropdown">Close (ปิด)</button>
                                </div>
                            </div>
                            <!-- /.panel-heading -->
                            <div class="panel-body" >
                                <div id="loading_search_form" class="table-responsive" >
                                    <div id="spinnerCE">
                                        <div class="bounce1"></div>
                                        <div class="bounce2"></div>
                                        <div class="bounce3"></div>
                                    </div>

                                </div>
                            </div>
                            <!-- /.panel-body -->
                        </div>
                        <!-- /.panel -->
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-10" id="show_temp_tble">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <i class="fa fa-bar-chart-o fa-fw"></i> View/Edit Invoice
                                <div class="pull-right">
                                    <button type="button" class="btn btn-primary btn-xs" style="width: 150px" data-toggle="dropdown" id="create_invoice_press">Download Invoice</button>
                                    <!--                                    <button type="button" class="btn btn-primary btn-xs" style="width: 150px;" data-toggle="dropdown" id="create_receipt_press">Create Receipt</button>
                                                                        <button type="button" class="btn btn-primary btn-xs" style="width: 150px;" data-toggle="dropdown" id="create_progressive_press">Create Progressive</button>-->
                                    <button type="button" id="close_viewedit_tbl" class="btn btn-default btn-xs" style="width: 100px" data-toggle="dropdown">Close (ปิด)</button>
                                </div>
                            </div>
                            <!-- /.panel-heading -->
                            <div class="panel-body" >
                                <div id="loading_viewedit_table" class="table-responsive" >
                                    <div id="spinnerCE">
                                        <div class="bounce1"></div>
                                        <div class="bounce2"></div>
                                        <div class="bounce3"></div>
                                    </div>

                                </div>
                            </div>
                            <!-- /.panel-body -->
                        </div>
                        <!-- /.panel -->
                    </div>
                </div>
                <div class="row" id="project_tbl_content">                    
                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <i class="fa fa-bar-chart-o fa-fw"></i> All Generate Billing

                                <div class="pull-right">

                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                                            Actions
                                            <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu pull-right" role="menu">
                                            <li>
                                                <a href="#" id="create_new_billing_btn"><i class = "fa fa-building-o"></i> Create New Billing (สร้างใบเสร็จใหม่)</a>
                                                <a href="#"><i class = "fa fa-search"></i> Search Billing (ค้นหา Billing)</a>
                                            </li>
                                        </ul>                                        
                                    </div>

                                </div>
                            </div>
                            <!-- /.panel-heading -->
                            <div class="panel-body">
                                <div class="table-responsive" id="loading_project">
                                    <div class="spinner">
                                        <div class="bounce1"></div>
                                        <div class="bounce2"></div>
                                        <div class="bounce3"></div>
                                    </div>
                                </div>
                            </div>
                            <!-- /.panel-body -->
                        </div>
                        <!-- /.panel -->
                    </div>
                </div>

            </div>
            <!-- /.row -->
        </div>
        <!-- /#page-wrapper -->

        <span id="hello_span"></span>
        <!-- jQuery Version 1.11.0 -->
        <script src="../js/jquery-1.11.0.js"></script>

        <!-- Bootstrap Core JavaScript -->
        <script src="../js/bootstrap.min.js"></script>

        <!-- Metis Menu Plugin JavaScript -->
        <script src="../js/plugins/metisMenu/metisMenu.min.js"></script>
        <script src="../js/jquery.toastmessage.js"></script>
        <!-- Custom Theme JavaScript -->
        <script src="../js/sb-admin-2.js"></script>
        <link rel="stylesheet" type="text/css" href="../css/jquery-ui-1.10.4.custom.css" />
        <script type="text/javascript" src="../js/jquery-ui-1.10.4.custom.js"></script>
        <script type="text/javascript" src="../js/jquery.multiselect.js"></script>
        <script type="text/javascript" src="../js/jquery.blockUI.js"></script>
        <script type="text/javascript" src="../js/jszip.js"></script>
        <link rel="stylesheet" type="text/css" href="../css/jquery.multiselect.css" />
        <style type="text/css">
            .status {
                padding-top: 2px;
                padding-left: 8px;
                vertical-align: top;
                width: 246px;
                white-space: nowrap;
            }
            .textfield {
                width: 150px;
            }
            label.error {
                background:url("../images/unchecked.gif") no-repeat 0px 0px;
                padding-left: 16px;
                padding-bottom: 2px;
                font-weight: bold;
                color: #EA5200;
            }
            label.checked {
                background:url("../images/checked.gif") no-repeat 0px 0px;
            }
            .success_msg {
                font-weight: bold;
                color: #0060BF;
                margin-left: 19px;
            }
        </style>
        <script type="text/javascript">

            var statusNa = 1;
            var isCreate = 1;
            var create_type = "";
            var create_receipt = "";
            var create_progressive = "";
            $(document).ready(function() {
                $("#accordion").accordion();
                updateUpperMenu();
                $("#loading_project").load("project_table_result.php", function() {

                });
                $("#search_panel").hide();
                $("#show_temp_tble").hide();
                $("#create_new_billing_btn").click(function() {
                    $("#loading_search_form").load("create-edit_form.php", function() {
                        $("#search_panel").show();
                    });
                    isCreate = 1;
                });

                $("#close_search_panel").click(function() {
                    $("#loading_search_form").empty();
                    $("#search_panel").hide();
                    isCreate = 1;
                });
                $("#close_viewedit_tbl").click(function() {
                    var jqxhr = $.post("../model/com.qrc.mgr.controller/DeleteInvoiceDetailTemp.php");
                    jqxhr.success(function(resp) {
                        if (resp == 1) {
                            $("#loading_viewedit_table").empty();
                            $("#show_temp_tble").hide();
                        } else {
                            alert(resp + "");
                        }
                    });

                });
                $("#logout_click").click(function() {
                    var jqxhr = $.post("../model/LogoutDesSession.php");
                    jqxhr.success(function(data) {
                        alert(data);
                        window.location.assign("../index.php")
                    });
                    jqxhr.error(function() {
                        alert("ไม่สามารถติดต่อกับ Server ได้");
                    });
                });
                $("#create_progressive_press").click(function() {
                    if (statusNa == 3) {
                        var jqxhr = $.post("../model/com.qrc.mgr.controller/VerifyNumberOfRow.php");
                        jqxhr.success(function(responseCheck) {
                            if (responseCheck == "200") {
                                var custId = $("#customer_id_pgs").val();
                                var inv_type = $("input[name=create_type_pgs]:checked").val();
                                var inv_code = $("#pgs_code").val();
                                if ($("#inv_status").val() == "") {
                                    var data = "inv_code=" + $("#pgs_code").val() + "&" +
                                            "customer_id=" + $("#customer_id_pgs").val() + "&" +
                                            "multi_sel_project_name=" + $("#multi_sel_project_name_pgs").val() + "&" +
                                            "wo_status=" + $("#wo_status_pgs").val() + "&" +
                                            "order_type_id=" + $("#order_type_id_pgs").val() + "&" +
                                            "inv_status=44002&" +
                                            "start_date=" + $("#start_date_pgs").val() + "&" +
                                            "end_date=" + $("#end_date_pgs").val();
                                } else {
                                    var data = "inv_code=" + $("#pgs_code").val() + "&" +
                                            "customer_id=" + $("#customer_id_pgs").val() + "&" +
                                            "multi_sel_project_name=" + $("#multi_sel_project_name_pgs").val() + "&" +
                                            "wo_status=" + $("#wo_status_pgs").val() + "&" +
                                            "order_type_id=" + $("#order_type_id_pgs").val() + "&" +
                                            "inv_status=" + $("#inv_status_pgs").val() + "&" +
                                            "start_date=" + $("#start_date_pgs").val() + "&" +
                                            "end_date=" + $("#end_date_pgs").val();
                                }

                                var jqxhr = $.post("../model/com.qrc.mgr.controller/ManageBillingFile.php?" + data);
                                jqxhr.success(function(response) {
                                    if (response == "200") {
                                        $().toastmessage('showSuccessToast', "สามารถสร้างใบเสร็จได้สำเร็จ");
                                        $("#loading_project").load("project_table_result.php", function() {
                                            $(".spinner").hide();
                                            $("#search_panel").hide();
                                            $("#show_temp_tble").hide();
                                            updateUpperMenu();
                                            window.location = 'pgs_page_generate_download.php?customer_id=' + custId + "&inv_type=" + inv_type + "&inv_code=" + inv_code;
                                        });
                                    } else {
                                        $().toastmessage('showWarningToast', "ไม่สามารถสร้างใบเสร็จได้ : " + response);
                                    }
                                });
                            } else {
                                $().toastmessage('showWarningToast', responseCheck);
                            }
                        });
                    }
                });
                $("#create_receipt_press").click(function() {
                    if (statusNa == 2) {
                        var jqxhr = $.post("../model/com.qrc.mgr.controller/VerifyNumberOfRow.php");
                        jqxhr.success(function(responseCheck) {
                            if (responseCheck == "200") {
                                var custId = $("#customer_id_rep").val();
                                var inv_type = $("input[name=create_type_rep]:checked").val();
                                var inv_code = $("#rep_code").val();
                                if ($("#inv_status").val() == "") {
                                    var data = "inv_code=" + $("#rep_code").val() + "&" +
                                            "customer_id=" + $("#customer_id_rep").val() + "&" +
                                            "multi_sel_project_name=" + $("#multi_sel_project_name_rep").val() + "&" +
                                            "wo_status=" + $("#wo_status_rep").val() + "&" +
                                            "order_type_id=" + $("#order_type_id_rep").val() + "&" +
                                            "inv_status=44002&" +
                                            "start_date=" + $("#start_date_rep").val() + "&" +
                                            "end_date=" + $("#end_date_rep").val();
                                } else {
                                    var data = "inv_code=" + $("#rep_code").val() + "&" +
                                            "customer_id=" + $("#customer_id_rep").val() + "&" +
                                            "multi_sel_project_name=" + $("#multi_sel_project_name_rep").val() + "&" +
                                            "wo_status=" + $("#wo_status_rep").val() + "&" +
                                            "order_type_id=" + $("#order_type_id_rep").val() + "&" +
                                            "inv_status=" + $("#inv_status_rep").val() + "&" +
                                            "start_date=" + $("#start_date_rep").val() + "&" +
                                            "end_date=" + $("#end_date_rep").val();
                                }

                                var jqxhr = $.post("../model/com.qrc.mgr.controller/ManageBillingFile.php?" + data);
                                jqxhr.success(function(response) {
                                    if (response == "200") {
                                        $().toastmessage('showSuccessToast', "สามารถสร้างใบเสร็จได้สำเร็จ");
                                        $("#loading_project").load("project_table_result.php", function() {
                                            $(".spinner").hide();
                                            $("#search_panel").hide();
                                            $("#show_temp_tble").hide();
                                            updateUpperMenu();
                                            window.location = 'receipt_page_generate_download.php?customer_id=' + custId + "&inv_type=" + inv_type + "&inv_code=" + inv_code;
                                        });
                                    } else {
                                        $().toastmessage('showWarningToast', "ไม่สามารถสร้างใบเสร็จได้ : " + response);
                                    }
                                });
                            } else {
                                $().toastmessage('showWarningToast', responseCheck);
                            }
                        });
                    }
                });
                $("#create_invoice_press").click(function() {
                    if (statusNa == 1) {
                        $.blockUI({
                            theme: true,
                            title: 'Loading...',
                            message: 'Files are generating...<img src="../images/loader.gif" />'
                        });
                        var jqxhr = $.post("../model/com.qrc.mgr.controller/VerifyNumberOfRow.php");
                        jqxhr.success(function(responseCheck) {
                            if (responseCheck == "200") {
                                var custId = $("#customer_id").val();
                                var inv_type = $("input[name=create_type]:checked").val();
                                var inv_code = $("#inv_code").val();
                                if ($("#inv_status").val() == "") {
                                    var data = "inv_code=" + $("#inv_code").val() + "&" +
                                            "customer_id=" + $("#customer_id").val() + "&" +
                                            "multi_sel_project_name=" + $("#multi_sel_project_name").val() + "&" +
                                            "wo_status=" + $("#wo_status").val() + "&" +
                                            "order_type_id=" + $("#order_type_id").val() + "&" +
                                            "inv_status=44002&" +
                                            "start_date=" + $("#start_date").val() + "&" +
                                            "end_date=" + $("#end_date").val();
                                } else {
                                    var data = "inv_code=" + $("#inv_code").val() + "&" +
                                            "customer_id=" + $("#customer_id").val() + "&" +
                                            "multi_sel_project_name=" + $("#multi_sel_project_name").val() + "&" +
                                            "wo_status=" + $("#wo_status").val() + "&" +
                                            "order_type_id=" + $("#order_type_id").val() + "&" +
                                            "inv_status=" + $("#inv_status").val() + "&" +
                                            "start_date=" + $("#start_date").val() + "&" +
                                            "end_date=" + $("#end_date").val();
                                }

                                var jqxhr = $.post("../model/com.qrc.mgr.controller/ManageBillingFile.php?" + data);
                                jqxhr.success(function(response) {
                                    if (response == "200") {
                                        $("#loading_project").load("project_table_result.php", function() {
                                            $(".spinner").hide();
                                            $("#search_panel").hide();
                                            $("#show_temp_tble").hide();
                                            updateUpperMenu();
                                            window.location = 'billing_page_generate_download.php?customer_id=' + custId + "&inv_type=" + inv_type + "&inv_code=" + inv_code;
                                            var millisecondsToWait = 1000;
                                            setTimeout(function() {
                                                window.location = 'receipt_page_generate_download.php?customer_id=' + custId + "&inv_type=" + inv_type + "&inv_code=" + inv_code;
                                            }, millisecondsToWait);
                                            $.unblockUI({
                                                onUnblock: function() {
                                                    $().toastmessage('showSuccessToast', "สามารถสร้างใบเสร็จได้สำเร็จ");
                                                }
                                            });
                                        });
                                    } else {
                                        $().toastmessage('showWarningToast', "ไม่สามารถสร้างใบเสร็จได้ : " + response);
                                    }
                                });
                            } else {
                                $().toastmessage('showWarningToast', responseCheck);
                            }
                        });
                    }
                });
                $("#create_billing").click(function() {
                    if (statusNa == 1) {
                        if ($("#multi_sel_project_name").val() == null) {
                            $().toastmessage('showWarningToast', "Please select at least one project");
                        } else {

                            $("#create_invoice_press").prop('disabled', false);
                            $("#create_receipt_press").prop('disabled', true);
                            $("#create_progressive_press").prop('disabled', true);

                            if ($("#inv_status").val() == "") {
                                //inv_status always open
                                var data = "inv_code=" + $("#inv_code").val() + "&" +
                                        "customer_id=" + $("#customer_id").val() + "&" +
                                        "multi_sel_project_name=" + $("#multi_sel_project_name").val() + "&" +
                                        "wo_status=" + $("#wo_status").val() + "&" +
                                        "order_type_id=" + $("#order_type_id").val() + "&" +
                                        "create_type=" + create_type + "&" +
                                        "inv_status=44002&" +
                                        "create_receive=" + create_receipt + "&" +
                                        "create_progressive=" + create_progressive + "&" +
                                        "start_date=" + $("#start_date").val() + "&" +
                                        "end_date=" + $("#end_date").val() + "&" +
                                        "isCreate=" + isCreate;
                            } else {
                                var data = "inv_code=" + $("#inv_code").val() + "&" +
                                        "customer_id=" + $("#customer_id").val() + "&" +
                                        "multi_sel_project_name=" + $("#multi_sel_project_name").val() + "&" +
                                        "wo_status=" + $("#wo_status").val() + "&" +
                                        "order_type_id=" + $("#order_type_id").val() + "&" +
                                        "create_type=" + create_type + "&" +
                                        "inv_status=" + $("#inv_status").val() + "&" +
                                        "create_receive=" + create_receipt + "&" +
                                        "create_progressive=" + create_progressive + "&" +
                                        "start_date=" + $("#start_date").val() + "&" +
                                        "end_date=" + $("#end_date").val() + "&" +
                                        "isCreate=" + isCreate;
                            }
                            var jqxhr = $.post("../model/com.qrc.mgr.controller/SaveBillingHeader.php?" + data);
                            jqxhr.success(function(resp) {
                                if (resp == 1) {
                                    $("#loading_viewedit_table").load("billing_page_generate.php", function() {
                                        $("#show_temp_tble").show();
                                        $('html,body').animate({scrollTop: $('#loading_viewedit_table').offset().top}, 'slow');
                                    });
                                } else if (resp == "") {
                                    alert("No Data Found");
                                    $("#loading_viewedit_table").empty();
                                    $("#show_temp_tble").hide();
                                } else if (resp == "406") {
                                    alert("Please select project");
                                    $("#loading_viewedit_table").empty();
                                    $("#show_temp_tble").hide();
                                } else {
                                    alert(resp);
                                }
                            });
                        }
                    }
                    if (statusNa == 2) {
                        if ($("#multi_sel_project_name_rep").val() == null) {
                            $().toastmessage('showWarningToast', "Please select at least one project");
                        } else if ($("input[name=create_type_rep]:checked").val() == undefined) {
                            //alert("Please select invoice type");
                            $().toastmessage('showWarningToast', "Please select Receipt type");
                        } else {
                            $("#create_invoice_press").prop('disabled', true);
                            $("#create_receipt_press").prop('disabled', false);
                            $("#create_progressive_press").prop('disabled', true);
                            if ($("#inv_status_rep").val() == "") {
                                var data = "inv_code=" + $("#rep_code").val() + "&" +
                                        "customer_id=" + $("#customer_id_rep").val() + "&" +
                                        "multi_sel_project_name=" + $("#multi_sel_project_name_rep").val() + "&" +
                                        "wo_status=" + $("#wo_status_rep").val() + "&" +
                                        "order_type_id=" + $("#order_type_id_rep").val() + "&" +
                                        "create_type=" + $("input[name=create_type_rep]:checked").val() + "&" +
                                        "inv_status=44002&" +
                                        "start_date=" + $("#start_date_rep").val() + "&" +
                                        "end_date=" + $("#end_date_rep").val();
                            } else {
                                var data = "inv_code=" + $("#rep_code").val() + "&" +
                                        "customer_id=" + $("#customer_id_rep").val() + "&" +
                                        "multi_sel_project_name=" + $("#multi_sel_project_name_rep").val() + "&" +
                                        "wo_status=" + $("#wo_status_rep").val() + "&" +
                                        "order_type_id=" + $("#order_type_id_rep").val() + "&" +
                                        "create_type=" + $("input[name=create_type_rep]:checked").val() + "&" +
                                        "inv_status=" + $("#inv_status_rep").val() + "&" +
                                        "start_date=" + $("#start_date_rep").val() + "&" +
                                        "end_date=" + $("#end_date_rep").val();
                            }
                            var jqxhr = $.post("../model/com.qrc.mgr.controller/SaveBillingHeader.php?" + data);
                            jqxhr.success(function(resp) {
                                if (resp == 1) {
                                    $("#loading_viewedit_table").load("billing_page_generate.php", function() {
                                        $("#show_temp_tble").show();
                                        $('html,body').animate({scrollTop: $('#loading_viewedit_table').offset().top}, 'slow');
                                    });
                                } else if (resp == "") {
                                    alert("No Data Found");
                                    $("#loading_viewedit_table").empty();
                                    $("#show_temp_tble").hide();
                                } else if (resp == "406") {
                                    alert("Please select project");
                                    $("#loading_viewedit_table").empty();
                                    $("#show_temp_tble").hide();
                                } else {
                                    alert(resp);
                                }
                            });
                        }
                    }
                    if (statusNa == 3) {
                        if ($("#multi_sel_project_name_pgs").val() == null) {
                            $().toastmessage('showWarningToast', "Please select at least one project");
                        } else if ($("input[name=create_type_pgs]:checked").val() == undefined) {
                            //alert("Please select invoice type");
                            $().toastmessage('showWarningToast', "Please select progressive type");
                        } else {
                            $("#create_invoice_press").prop('disabled', true);
                            $("#create_receipt_press").prop('disabled', true);
                            $("#create_progressive_press").prop('disabled', false);

                            if ($("#inv_status_pgs").val() == "") {
                                var data = "inv_code=" + $("#pgs_code").val() + "&" +
                                        "customer_id=" + $("#customer_id_pgs").val() + "&" +
                                        "multi_sel_project_name=" + $("#multi_sel_project_name_pgs").val() + "&" +
                                        "wo_status=" + $("#wo_status_pgs").val() + "&" +
                                        "order_type_id=" + $("#order_type_id_pgs").val() + "&" +
                                        "create_type=" + $("input[name=create_type_pgs]:checked").val() + "&" +
                                        "inv_status=44002&" +
                                        "start_date=" + $("#start_date_pgs").val() + "&" +
                                        "end_date=" + $("#end_date_pgs").val();
                            } else {
                                var data = "inv_code=" + $("#pgs_code").val() + "&" +
                                        "customer_id=" + $("#customer_id_pgs").val() + "&" +
                                        "multi_sel_project_name=" + $("#multi_sel_project_name_pgs").val() + "&" +
                                        "wo_status=" + $("#wo_status_pgs").val() + "&" +
                                        "order_type_id=" + $("#order_type_id_pgs").val() + "&" +
                                        "create_type=" + $("input[name=create_type_pgs]:checked").val() + "&" +
                                        "inv_status=" + $("#inv_status_pgs").val() + "&" +
                                        "start_date=" + $("#start_date_pgs").val() + "&" +
                                        "end_date=" + $("#end_date_pgs").val();
                            }
                            var jqxhr = $.post("../model/com.qrc.mgr.controller/SaveBillingHeader.php?" + data);
                            jqxhr.success(function(resp) {
                                if (resp == 1) {
                                    $("#loading_viewedit_table").load("billing_page_generate.php", function() {
                                        $("#show_temp_tble").show();
                                        $('html,body').animate({scrollTop: $('#loading_viewedit_table').offset().top}, 'slow');
                                    });
                                } else if (resp == "") {
                                    alert("No Data Found");
                                    $("#loading_viewedit_table").empty();
                                    $("#show_temp_tble").hide();
                                } else if (resp == "406") {
                                    alert("Please select project");
                                    $("#loading_viewedit_table").empty();
                                    $("#show_temp_tble").hide();
                                } else {
                                    alert(resp);
                                }
                            });
                        }
                    }
                });
            });
            function updateUpperMenu() {
                $("#load_all_status").load("../menu-page/load_all_status_page.php", function() {
                    setTimeout(function()
                    {
                        //Update Project
                        $("#update_project_status").load("../menu-page/project_menu_page.php", function() {
                            $(".spinner").hide();
                        });
                        //Update TaskAssignment 
                        $("#update_assign_status").load("../menu-page/taskassign_menu_page.php", function() {
                            $(".spinner").hide();
                        });
                        //Update po inspection 
                        $("#update_poinspection_status").load("../menu-page/poinspection_menu_page.php", function() {
                            $(".spinner").hide();
                        });
                        //Update team member update_team_member_status
                        $("#update_team_member_status").load("../menu-page/teammember_menu_page.php", function() {
                            $(".spinner").hide();
                        });
                        $("#left-menu-load").load("../leftmenu-page/index-left-menu.php");
                    }
                    , 100);
                });

            }
            function generateBilling(invCode, custId, inv_type) {
                var check = invCode.split("-")[1].substring(0, 3);
                if (check == "INV") {
                    var jqxhr = $.post("../model/com.qrc.mgr.controller/SavingToInvoiceDetail.php?inv_code=" + invCode);
                    jqxhr.success(function(respInv) {
                        if (respInv == 0) {
                            if (confirm("This invoice has been generated, Continue to generate this invoice?") == true) {
                                window.location = 'billing_page_generate_download.php?customer_id=' + custId + "&inv_type=" + inv_type + "&inv_code=" + invCode;
                            }
                        }
                    });

                }
                if (check == "REP") {
                    if (confirm("This receipt has been generated, Continue to generate this receipt?") == true) {
                        window.location = 'receipt_page_generate_download.php?customer_id=' + custId + "&inv_type=" + inv_type + "&inv_code=" + invCode;
                    }
                }
//                if (check == "PGS") {
//                    if (confirm("This progressive has been generated, Continue to generate this progressive?") == true) {
//                        window.location = 'pgs_page_generate_download.php?customer_id=' + custId + "&inv_type=" + inv_type + "&inv_code=" + invCode;
//                    }
//                }
            }
            function generateProgressive(invCode, custId, inv_type) {
                if (confirm("This progressive has been generated, Continue to generate this progressive?") == true) {
                    window.location = 'pgs_page_generate_download.php?customer_id=' + custId + "&inv_type=" + inv_type + "&inv_code=" + invCode;
                }
            }
            function deleteBilling(inv_id) {
                if (confirm("Are you sure?"))
                {
                    // blockPage();
                    var jqxhr = $.post("../model/DeleteInvoice.php?inv_id=" + inv_id);
                    jqxhr.success(function(data) {
                        if (data == 1) {
                            $("#loading_project").load("project_table_result.php", function() {
                                $(".spinner").hide();
                                $("#create_edit_panel").hide();
                                $("#loading_ce_form").empty();
                                updateUpperMenu();
                                $('html,body').animate({scrollTop: $('#loading_project').offset().top}, 'fast');
                            });
                            $().toastmessage('showSuccessToast', 'ลบข้อมูลใบเสร็จเรียบร้อยแล้ว');

                        } else {
                            $().toastmessage('showWarningToast', "ไม่สามารถลบข้อมูลใบเสร็จได้");
                        }
                    });
                    jqxhr.error(function(data) {
                        window.location.replace("error.php?error_msg=" + data);
                    });
                }
                else
                {
                    e.preventDefault();
                }
            }
            function deleteSubLevel(tempDetailID) {
                var jqxhr = $.post("../model/com.qrc.mgr.controller/DeleteSubInvoiceService.php?tempDetailId=" + tempDetailID);
                jqxhr.success(function(data) {
                    if (data == 200) {
                        $().toastmessage('showSuccessToast', 'ลบข้อมูลใบเสร็จเรียบร้อยแล้ว');
                        $("#loading_viewedit_table").load("billing_page_generate.php", function() {
                            $("#show_temp_tble").show();
                            $('html,body').animate({scrollTop: $('#loading_viewedit_table').offset().top}, 'fast');
                        });
                    } else {
                        $().toastmessage('showWarningToast', "ไม่สามารถลบข้อมูลใบเสร็จได้");
                    }
                });
                jqxhr.error(function() {
                    alert("ไม่สามารถติดต่อกับ Server ได้");
                });
            }
            function deleteFirstLevel(tempDetailID) {
                var jqxhr = $.post("../model/com.qrc.mgr.controller/DeleteFirstInvoiceService.php?tempDetailId=" + tempDetailID);
                jqxhr.success(function(data) {
                    if (data == 200) {
                        $().toastmessage('showSuccessToast', 'ลบข้อมูลใบเสร็จเรียบร้อยแล้ว');
                        $("#loading_viewedit_table").load("billing_page_generate.php", function() {
                            $("#show_temp_tble").show();
                            $('html,body').animate({scrollTop: $('#loading_viewedit_table').offset().top}, 'slow');
                        });
                    } else {
                        $().toastmessage('showWarningToast', "ไม่สามารถลบข้อมูลใบเสร็จได้");
                    }
                });
                jqxhr.error(function() {
                    alert("ไม่สามารถติดต่อกับ Server ได้");
                });
            }
            function updateStatus(status) {
                statusNa = status;
                $("#show_temp_tble").hide();
            }
            
        </script>        
    </body>

</html>
